package top.songxs.yygh.hosp.user.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.songxs.yygh.common.result.R;
import top.songxs.yygh.enums.AuthStatusEnum;
import top.songxs.yygh.model.user.UserInfo;
import top.songxs.yygh.hosp.user.service.UserInfoService;
import top.songxs.yygh.hosp.user.util.AuthContextHolder;
import top.songxs.yygh.vo.user.LoginVo;
import top.songxs.yygh.vo.user.UserAuthVo;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@RestController
@RequestMapping("api/user")
public class UserInfoApiController {

    @Autowired
    UserInfoService userInfoService;

    @PostMapping("login")
    public R login(@RequestBody LoginVo loginVo) {
        Map<String, Object> info = userInfoService.login(loginVo);
        return R.ok().data(info);
    }

    //用户认证接口
    @PostMapping("auth/userAuth")
    public R userAuth(@RequestBody UserAuthVo userAuthVo, HttpServletRequest request) {
        Long userId = AuthContextHolder.getUserId(request);
        userInfoService.userAuth(userId,userAuthVo);
        return R.ok();
    }

    //根据用户id获取用户信息接口
    @GetMapping("auth/getUserInfo")
    public R getUserInfo(HttpServletRequest request){
        Long userId = AuthContextHolder.getUserId(request);
        UserInfo userInfo = userInfoService.getById(userId);

        //从枚举类中根据认证状态查出对应字符串
        Integer authStatus = userInfo.getAuthStatus();
        String authStatusString = AuthStatusEnum.getStatusNameByStatus(authStatus);
        userInfo.getParam().put("authStatusString",authStatusString);


        return R.ok().data("userInfo",userInfo);
    }




















}
